﻿using System;
using System.IO;
using System.Windows.Forms;
using Alchemy;
using Alchemy.Classes;
using System.Net;

namespace shserver
{
    public partial class frmMain : Form
    {
        //private static Reporter rp = Reporter.Instance;

        WebSocketServer wsServer;
        SignalHound sh = SignalHound.Instance;

        public frmMain()
        {
            InitializeComponent();

            wsServer = new WebSocketServer(81, IPAddress.Any)
            {
                OnReceive = OnReceive,
                OnSend = OnSend,
                OnConnect = OnConnected,
                OnConnected = OnConnect,
                OnDisconnect = OnDisconnect,
                TimeOut = new TimeSpan(0, 5, 0)
            };
            //wsServer.ListenAddress = IPAddress.Loopback;
            //wsServer.Restart();
            wsServer.Start();
        }
        static void OnConnect(UserContext ws)
        {

        }

        static void OnConnected(UserContext ws)
        {
            Reporter.log("Client connected from: " + ws.ClientAddress.ToString());
            SignalHound sh = SignalHound.Instance;
            Reporter.log("SHAPI Initializating...");
            sh.init();
            Reporter.log("SHAPI Initializated");
        }

        static void OnReceive(UserContext ws)
        {
            SignalHound sh = SignalHound.Instance;
            
            switch ((int)ws.Data)
            {
                case 1: // Configure
                    sh.configure();
                    Reporter.log("SHAPI Configured");
                    break;
                case 2: // Reset
                    sh.reset();
                    Reporter.log("SHAPI Reseted");
                    break;
                case 3: // Plot
                    sh.configure();
                    Reporter.log("SHAPI Reseted");
                    while(true){
                        ws.Send("data");
                    }
                    break;
            }
        }

        static void OnSend(UserContext ws)
        {
        }

        static void OnDisconnect(UserContext ws)
        {
            Reporter.log("Client disconnected");
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            //SignalHound sh = SignalHound.Instance;
            //sh.reset();
            wsServer.Stop();
            wsServer.Dispose();
        }

        //SHAPI Test Actions Buttons

        private void btnSHReset_Click(object sender, EventArgs e)
        {
            sh.reset();
            Reporter.log("SHAPI Reseted (Test)");
        }

        private void btnSHConfigure_Click(object sender, EventArgs e)
        {
            sh.configure();
            Reporter.log("SHAPI Configured (Test)");
        }

        private void btnSHInit_Click(object sender, EventArgs e)
        {
            Reporter.log("SHAPI Initializating... (Test)");
            sh.init();
            Reporter.log("SHAPI Initializated (Test)");
        }

        private void btnSHData_Click(object sender, EventArgs e)
        {
            String myValStr = "";
            double[] data = sh.getData();

            foreach(double d in data)
            {
                myValStr += d.ToString() + ";";
            }
            Reporter.log(myValStr);
        }
    }
}
